/*
 * Copyright (C) 2010 The Android Open Source Project 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package org.lhlzi.ithink;

import org.lhlzi.ithink.R;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

// manage contact
// http://dev.10086.cn/cmdn/wiki/index.php?doc-view-3607.html

public class NumberSearch extends Activity implements View.OnClickListener, View.OnLongClickListener{
	
	private TextView numText = null;
	private TextView numArea = null;
	private String number = "";
	//private NumberDatabase mNumberDb;
	private String mArea = null;
	
	private boolean mLongClick = false;
	
	private static DataCacheWrapper dataCacheWrapper = DataCacheWrapper.getInstance();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//mNumberDb = new NumberDatabase(this);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.dialer);
		
		
		ImageButton button = (ImageButton)findViewById(R.id.one);
		button.setOnClickListener(this);
		
		button = (ImageButton)findViewById(R.id.two);
		button.setOnClickListener(this);
		
		button = (ImageButton)findViewById(R.id.three);
		button.setOnClickListener(this);
		
		button = (ImageButton)findViewById(R.id.four);
		button.setOnClickListener(this);
		button = (ImageButton)findViewById(R.id.five);
		button.setOnClickListener(this);
		
		button = (ImageButton)findViewById(R.id.six);
		button.setOnClickListener(this);
		
		button = (ImageButton)findViewById(R.id.seven);
		button.setOnClickListener(this);
		
		button = (ImageButton)findViewById(R.id.eight);
		button.setOnClickListener(this);
		
		button = (ImageButton)findViewById(R.id.nine);
		button.setOnClickListener(this);
		
		button = (ImageButton)findViewById(R.id.zore);
		button.setOnClickListener(this);
		button.setOnLongClickListener(this);
		
		button = (ImageButton)findViewById(R.id.star);
		button.setOnClickListener(this);
		
		button = (ImageButton)findViewById(R.id.sharp);
		button.setOnClickListener(this);
		
		button = (ImageButton)findViewById(R.id.backspace);
		button.setOnClickListener(this);
		button.setOnLongClickListener(this);
		
		button = (ImageButton)findViewById(R.id.call);
		button.setOnClickListener(this);
		
		button = (ImageButton)findViewById(R.id.add_contact);
		button.setOnClickListener(this);
		
		numText = (TextView)findViewById(R.id.num_container);
		numArea = (TextView)findViewById(R.id.num_area);
	}

	@Override
	public void onClick(View view) {
		
		if (!mLongClick) {
		
			switch(view.getId()) {
			case R.id.zore:
				number += "0";
				break;
			case R.id.one:
				number += "1";
				break;
			case R.id.two:
				number += "2";
				break;
			case R.id.three:
				number += "3";
				break;
			case R.id.four:
				number += "4";
				break;
			case R.id.five:
				number += "5";
				break;
			case R.id.six:
				number += "6";
				break;
			case R.id.seven:
				number += "7";
				break;
			case R.id.eight:
				number += "8";
				break;
			case R.id.nine:
				number += "9";
				break;
			case R.id.star:
				number +="*";
				break;
			case R.id.backspace:
				if (number != null && number.length() > 0)
					number = number.substring(0, number.length() - 1);
				break;
			case R.id.sharp:
				number += "#";
				break;
			case R.id.call:
				if (number.length() > 0)
					callNumber(number);
				break;
			case R.id.add_contact:
				break;
			}			
			
			mArea = dataCacheWrapper.revealNumber(number);
			
			numArea.setText(mArea.equals("unknown") ? "" : mArea);	
			
			numText.setText(Utils.formatNumber(number));
		}
		
		mLongClick = false;
		
	}

	@Override
	public boolean onLongClick(View view) {
		switch(view.getId()) {
		case R.id.backspace:
			number = "";
			numArea.setText("");
			break;
		case R.id.zore:
			number += "+";
			break;
		}
				
		mLongClick = true;
		numText.setText(number);
		return false;
	}
	
	private void callNumber(String number) {
		Intent intent = new Intent(Intent.ACTION_CALL);
		intent.setData(Uri.parse("tel:"+number));
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
	}
}
